<?php

namespace App\Services;

use App\Exports\CustomExport;
use App\Models\Cms_Custom;
use App\Models\Cms_Custom_Export_Log;
use App\Models\Cms_Custom_Follow;
use App\Traits\ResponseApi;
use Carbon\Carbon;
use Qiniu\Storage\UploadManager;
use App\Third\Qiniu;

class CustomService
{
    public function getList($params)
    {
        $result = Cms_Custom::getFilter($params)
            ->orderBy('created_at','desc')
            ->paginate($params['pageSize']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function getInfo($params)
    {
        return Cms_Custom::where('id',$params['mid'])->first();
    }

    public function insertCustom($params)
    {
        $params['sid']  = ResponseApi::getSnowflakeId();
        $params['sn']  = Cms_Custom::_generateSn();
        print_r($params['sn']);exit;
        $time = Carbon::now()->timestamp;
        $params['create_time']  = $time;
        $params['update_time']  = $time;
        $result = Cms_Custom::create($params);
        if($result)
            return true;
        else
            return false;
    }

    public function updateCustom($params)
    {
        $result = Cms_Custom::where('id',$params['id'])->update($params);
        if($result)
            return true;
        else
            return false;
    }

    public function updateCustomState($params)
    {
        if(is_string($params['ids']))
            $result = Cms_Custom::whereIn('id',explode(',',$params['ids']))
                ->update(['state' => $params['state']]);
        else
            $result = Cms_Custom::where('id',$params['ids'])
                ->update(['state' => $params['state']]);

        if($result)
            return true;
        else
            return false;
    }

    public function updateCustomStatus($params)
    {
        $result = Cms_Custom::find($params['id']);
        $result->status = $params['status'];
        if($result->save())
            return true;
        else
            return false;
    }

    public function updateCustomIntention($params)
    {
        $result = Cms_Custom::find($params['id']);
        $result->intention = $params['intention'];
        if($result->save())
            return true;
        else
            return false;
    }

    public function updateCustomDrow($params)
    {
        if(is_string($params['ids']))
            $result = Cms_Custom::whereIn('id',explode(',',$params['ids']))
                ->update(['drop' => 2,'droptype' => 1]);
        else
            $result = Cms_Custom::where('id',$params['ids'])
                ->update(['drop' => 2,'droptype' => 1]);

        if($result)
            return true;
        else
            return false;
    }

    public function customDel($params)
    {
        if(is_string($params['ids']))
            $result = Cms_Custom::whereIn('id',explode(',',$params['ids']))->update(['is_del'=>'Y']);
        else
            $result = Cms_Custom::where('id',$params['ids'])->update(['is_del','Y']);

        if($result)
            return true;
        else
            return false;
    }

    public function getFollows($params)
    {
        $result = Cms_Custom_Follow::getFilter($params)->Nodel()->orderBy('created_at','desc')->get();

        return $result;
    }

    public function getCustomReport()
    {
        $list = [
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
            ['username'=>'admin','data1'=>4,'data2'=>0,'data3'=>0,'data4'=>0,'data5'=>0,'data6'=>0,'data7'=>0,'data8'=>0],
        ];

        return ['list'=>$list];
    }

    public function insertFollow($params)
    {
        $result = Cms_Custom_Follow::create($params);

        if($result)
            return true;
        else
            return false;
    }

    public function exportLog()
    {
        return Cms_Custom_Export_Log::orderBy('id','desc')->get();
    }

    public function exportTemplate($params)
    {
        $file_name = 'export_template_'.date('Y-m-d_H-i-s',time()).'.xlsx';
        $result = Excel::download(new CustomExport($params), $file_name)
            ->getFile()
            ->getFileInfo();
        $filepath = $result->getPath().'/'.$result->getFilename();
        $uptoken = Qiniu::getToken();
        // 构建 UploadManager 对象
        $uploadMgr = new UploadManager();
        list($ret, $error) = $uploadMgr->putFile($uptoken, 'export/'.$file_name, $filepath);

        if ($error !== null) {
            return false;
        }
        $path = config('filesystems.disks.qiniu')['domain'].$ret['key'];

        Cms_Custom_Export_Log::create(['filename' => $file_name ,'fileurl' => $path]);

        return $path;
    }

}
